﻿using System;
using System.Data;
using DotNetCore.CAP;
using Microsoft.AspNetCore.Mvc;
using MySql.Data.MySqlClient;
using StackExchange.Redis;

namespace rabbitmqdemo.Controllers
{
    [Route("api/cappublish")]
    public class CapPublishController : Controller
    {
        private ICapPublisher _publisher;

        public CapPublishController(ICapPublisher publisher)
        {
            _publisher = publisher;
        }

        [Route("publish")]
        public Object Publish()
        {
            var connectionstring = "server=192.168.3.109;userid=root;pwd=Rp000000;port=3306;database=cap;SslMode=none;";

            using (IDbConnection conn = new MySqlConnection(connectionstring))
            {
                conn.Open();
                using (var tran = conn.BeginTransaction())
                {
                    _publisher.Publish("test1", new {Name = "ahs", Age = "21"}, tran);
                    tran.Commit();
                }
            }

            return "发送一条消息";
        }

        [Route("sub")]
        [CapSubscribe("test1")]
        public Object Sub()
        {
            Console.WriteLine("插入用户成功");
            return "这里是自动创建了消费者";
        }
    }
}